// pages/player/player.js
let musicList = [] // 整个歌单的所以歌曲信息
let playMusicIndex = 0 // 当前正在播放的歌曲的下标
let music = {} 
let onPlay = false
let musicId = 0
const app = getApp()
// 获取全局唯一的背景音频管理器
const backgroundAudioManager = wx.getBackgroundAudioManager()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picUrl: '',
    isPlaying: false,
    isShowLyric:false,
    isSameMusic:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 初始化页面
    musicId = options.musicId
    musicList = wx.getStorageSync('musicList')
    playMusicIndex = options.index
    music = musicList[playMusicIndex]
    this._loadMusicDetail(music)
  },

  // 加载歌曲信息并播放
  _loadMusicDetail(music){
    // 判断用户点进来的歌曲是否当前播放的歌曲是同一首
    if (music.id == app.getPlayingId()) {
      this.setData({
        isSameMusic: true
      })
    }else{
      this.setData({
        isSameMusic: false
      })
    }
    if (!this.data.isSameMusic) {
      backgroundAudioManager.stop()
    }
    // 设置顶部title
    wx.setNavigationBarTitle({
      title: music.name
    })
    this.setData({
      picUrl: music.al.picUrl
    })
    // 把播放音乐的id保存到全局
    app.setPlayingId(music.id)
    this.isPlaying()
    // 播放歌曲 
    if (!this.data.isSameMusic) {
      wx.showLoading({
        title: '歌曲加载中'
      })
      const musicUrl = `https://music.163.com/song/media/outer/url?id=${music.id}.mp3`;
      backgroundAudioManager.src = musicUrl
      backgroundAudioManager.title = music.name
      backgroundAudioManager.coverImgUrl = music.al.picUrl
      backgroundAudioManager.singer = music.ar[0].name
      backgroundAudioManager.epname = music.al.name
      this.savePlayHistory()
    }
  },
  // 监听音频播放事件
  isPlaying() {
    this.setData({
      isPlaying: true
    })
    wx.hideLoading()
  },
  isPause(){
    this.setData({
      isPlaying:false
    })
  },
  // 点击播放按钮切换状态
  togglePlaying(){
    if (this.data.isPlaying) {
      backgroundAudioManager.pause()
    }else{
      backgroundAudioManager.play()
    }
    this.setData({
      isPlaying: !this.data.isPlaying
    })
  },

  // 点击上一首
  onPrev(){
    playMusicIndex--
    if (playMusicIndex < 0) {
      playMusicIndex = musicList.length-1
    }
    this._loadMusicDetail(musicList[playMusicIndex])
  },

  // 点击下一首
  onNext(){
    playMusicIndex++
    if (playMusicIndex === musicList.length) {
      playMusicIndex = 0
    }
    this._loadMusicDetail(musicList[playMusicIndex])
  },

  // 展示歌词
  showLyric() {
    this.setData({
      isShowLyric: !this.data.isShowLyric
    })
  },

  // 保存播放历史
  savePlayHistory(){
    const openid = app.globalData.openid
    const historyArr = wx.getStorageSync(openid)
    let musicItem = musicList[playMusicIndex]
    let beHave = false
    for (let i = 0; i < historyArr.length; i++) {
      if(historyArr[i].id == musicItem.id){
        beHave = true
        break
      }
    }
    if (!beHave) {
      historyArr.unshift(musicItem)
      wx.setStorageSync(openid,historyArr)
    }
  },
      
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})